package com.lianxueda.drawing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lianxueda.drawing.domain.dto.CourseStyleQueryDTO;
import com.lianxueda.drawing.domain.pojo.CourseStyle;
import com.lianxueda.drawing.domain.pojo.Courses;
import com.lianxueda.drawing.domain.vo.PageVO;
import com.lianxueda.drawing.mapper.CourseStyleMapper;
import com.lianxueda.drawing.service.CourseStyleService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
* @author 29264
* @description 针对表【course_style】的数据库操作Service实现
* @createDate 2024-08-16 14:28:44
*/
@Service
public class CourseStyleServiceImpl extends ServiceImpl<CourseStyleMapper, CourseStyle>
    implements CourseStyleService{

    @Override
    public void updateViewCount(Integer id, Integer viewCount) {
        //根据id更新浏览量
        LambdaQueryWrapper<CourseStyle> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseStyle::getId,id);
        CourseStyle courseStyle = this.getOne(queryWrapper);
        courseStyle.setViewCount(viewCount);
        this.updateById(courseStyle);
    }

    @Override
    public PageVO<CourseStyle> searchPage(CourseStyleQueryDTO dto) {
            LambdaQueryWrapper<CourseStyle> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.ne(CourseStyle::getDeleted, 1);
            queryWrapper.like(StringUtils.hasText(dto.getContent()),CourseStyle::getContent,dto.getContent());
            if(dto.getCourseId()!=null){
                queryWrapper.eq(CourseStyle::getCourseId,dto.getCourseId());
            }
            if(dto.getStartPublishTime()!=null){
                queryWrapper.apply("date_format(publishTime,'%Y-%m-%d') >= {0}", dto.getStartPublishTime());
            }
            if(dto.getEndPublishTime()!=null){
                queryWrapper.apply("date_format(publishTime,'%Y-%m-%d') <= {0}", dto.getEndPublishTime());
            }
            IPage<CourseStyle> page = new Page<>(dto.getCurPage(), dto.getPageSize());
            queryWrapper.orderByDesc(CourseStyle::getId);
            queryWrapper.select(CourseStyle.class, info ->!info.getColumn().equals("deleted"));
            IPage<CourseStyle> pageResult = this.baseMapper.selectPage(page, queryWrapper);

            PageVO<CourseStyle> pageVO = new PageVO<>();
            pageVO.setCurrentPage(pageResult.getCurrent());
            pageVO.setPageSize(pageResult.getSize());
            pageVO.setTotal(pageResult.getTotal());
            pageVO.setList(pageResult.getRecords());
            return pageVO;
    }
}




